package com.example.chengww.myone.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.chengww.myone.R;
import com.example.chengww.myone.adapter.MyPagerAdapter;
import com.example.chengww.myone.anim.FrameAnimation;
import com.example.chengww.myone.bean.Constants;
import com.example.chengww.myone.bean.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * 引导页
 */
public class GuideActivity extends AppCompatActivity {

    private ViewPager viewPager;
    private LinearLayout layoutPointContainer;
    private List<View> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);


        layoutPointContainer = (LinearLayout) findViewById(R.id.layoutPointContainer);

        //viewPager的List
        list = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            FrameAnimation frameView = new FrameAnimation(this);

            ImageView imagePoint = new ImageView(this);
            imagePoint.setPadding(15,0,15,0);
            layoutPointContainer.addView(imagePoint);

            if (i == 0){
                frameView.setBitmapResoursID(Constants.read_guide);
                frameView.start();
                imagePoint.setImageResource(R.drawable.guide_checked_index);
            }else {
                imagePoint.setImageResource(R.drawable.guide_default_index);
            }
//            switch (i){
//                case 0:
//                    imageView.setBackgroundResource(R.drawable.read_guide);
//                    break;
//                case 1:
//                    imageView.setBackgroundResource(R.drawable.music_guide);
//                    break;
//                case 2:
//                    imageView.setBackgroundResource(R.drawable.movie_guide);
//                    break;
//                case 3:
//                    imageView.setBackgroundResource(R.drawable.one_guide);
//                    break;
//            }
//
//            animationDrawable = (AnimationDrawable) imageView.getBackground();
//            animationDrawable.start();//开始

            list.add(frameView);
        }

        viewPager = (ViewPager) findViewById(R.id.viewPager);
        MyPagerAdapter pagerAdapter = new MyPagerAdapter(list);
        viewPager.setAdapter(pagerAdapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                //关联引导页和圆点状态
                for (int i = 0; i < 4; i++) {
                    ImageView imagePoint = (ImageView) layoutPointContainer.getChildAt(i);
                    if (i == position){
                        imagePoint.setImageResource(R.drawable.guide_checked_index);
                    }else {
                        imagePoint.setImageResource(R.drawable.guide_default_index);
                    }
                }

                //开启每个页面动画
                FrameAnimation frameView = (FrameAnimation) list.get(position);
                switch (position){
                case 0:
                    frameView.setBitmapResoursID(Constants.read_guide);
                    break;
                case 1:
                    frameView.setBitmapResoursID(Constants.music_guide);
                    break;
                case 2:
                    frameView.setBitmapResoursID(Constants.movie_guide);
                    break;
                case 3:
                    layoutPointContainer.removeAllViews();
                    ImageView imageView = new ImageView(GuideActivity.this);
                    imageView.setImageResource(R.drawable.guide_into_one);
                    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    imageView.setBackgroundResource(R.drawable.background_into_one);
                    imageView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            //已点击，不再引导
                            setGuide(false);
                            Intent getIntent = getIntent();
                            ArrayList<Data> datas = getIntent.getParcelableArrayListExtra("datas");
                            Intent intent = new Intent(GuideActivity.this, MainActivity.class);
                            intent.putParcelableArrayListExtra("datas",datas);
                            startActivity(intent);
                            GuideActivity.this.finish();
                        }
                    });
                    layoutPointContainer.addView(imageView);
                    frameView.setBitmapResoursID(Constants.one_guide);
                    break;
            }

                frameView.start();//开始
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    /**
     * 设置是否再次进入引导页
     * @param isGuide
     */
    private void setGuide(boolean isGuide){
        SharedPreferences mSharedPreferences = getSharedPreferences("isFirstEntry",
                Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = mSharedPreferences.edit();
        editor.putBoolean("isGuide",isGuide);
        editor.commit();
    }
}
